package cn.edu.zut.controller;

import cn.edu.zut.domin.Reader;
import cn.edu.zut.service.IReaderService;
import com.github.pagehelper.PageInfo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/reader")
public class ReaderController {
    private ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    //获取对象
    private IReaderService readerService =(IReaderService) ac.getBean("readerServiceImpl");
    @RequestMapping("/findAll")
    //默认第一页,每页显示四个
    public ModelAndView findAll(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                                @RequestParam(name = "size", required = true, defaultValue = "4") int size) throws Exception {
        ModelAndView mv = new ModelAndView();
        List<Reader> readers = readerService.findAll(page, size);
        //分页Bean PageInfo就是一个分页的Bean
        PageInfo<Reader> pageInfo = new PageInfo<>(readers);
        //总页数等....
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("Reader-List");
        return mv;
    }
    @RequestMapping("/save")
    public String saveReader(Reader reader) throws Exception {
        readerService.saveReader(reader);
        return "redirect:findAll";
    }

    @RequestMapping("/update")
    public String updateReader(Reader reader) throws Exception {
        readerService.updateReader(reader);
        return "redirect:findAll";
    }
    @RequestMapping("/findReaderByName")
    public ModelAndView findByReaderName(String userName) throws Exception {
        System.out.println(userName);
        ModelAndView mv = new ModelAndView();
        List<Reader> readers=readerService.findReaderByName(userName);
        mv.addObject("readerList", readers);
        mv.setViewName("Reader-List");
        return mv;
    }
    @RequestMapping("/findReaderById")
    public ModelAndView findByReaderId(String readerId) throws Exception {
        System.out.println(readerId);
        ModelAndView mv = new ModelAndView();
        List<Reader> readers = readerService.findReaderById(readerId);
        mv.addObject("readerList", readers);
        mv.setViewName("Reader-List");
        return mv;
    }
    @RequestMapping("/findReaderByTel")
    public ModelAndView findByReaderTel(String tel) throws Exception {
        System.out.println(tel);
        ModelAndView mv = new ModelAndView();
        List<Reader> readers = readerService.findByReaderTel(tel);
        mv.addObject("readerList", readers);
        mv.setViewName("Reader-List");
        return mv;
    }
    @RequestMapping("/delete")
    public String deleteBookById(String readerId) throws Exception {
        readerService.deleteReader(readerId);

        //删除后回到全部信息页面
        return "redirect:findAll";
    }
    @RequestMapping("/findReaderByMOHU")
    public ModelAndView findByReaderNameMOHU(String name) throws Exception {
        System.out.println(name);
        ModelAndView mv = new ModelAndView();
        List<Reader> readers = readerService.findReaderByNameMOHU(name);
        mv.addObject("readerList", readers);
        mv.setViewName("Reader-List");
        return mv;
    }


}
